#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tkinter import *
import hashlib
import time

import RSA
import ECC
import RSAAES
import ECCAES
import DH
LOG_LINE_NUM = 0

class MY_GUI():
    def __init__(self,init_window_name):
        self.init_window_name = init_window_name


    #设置窗口
    def set_init_window(self):
        self.init_window_name.title("吴祥凡-20190581310182签密工具工具_v1.2")           #窗口名
        #self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小，+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.geometry('1068x681+10+10')
        #self.init_window_name["bg"] = "pink"                                    #窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        #self.init_window_name.attributes("-alpha",0.9)                          #虚化，值越小虚化程度越高
        #标签
        self.init_data_label = Label(self.init_window_name, text="明文")
        self.init_data_label.grid(row=0, column=0)
        self.result_data_label = Label(self.init_window_name, text="输文")
        self.result_data_label.grid(row=0, column=12)
        self.log_label = Label(self.init_window_name, text="密钥")
        self.log_label.grid(row=12, column=0)
        #文本框
        self.init_data_Text = Text(self.init_window_name, width=67, height=35)  #原始数据录入框
        self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
        self.result_data_Text = Text(self.init_window_name, width=70, height=49)  #处理结果展示
        self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
        self.log_data_Text = Text(self.init_window_name, width=66, height=9)  # 日志框
        self.log_data_Text.grid(row=13, column=0, columnspan=10)
        #按钮
        self.str_trans_to_md5_button = Button(self.init_window_name, text="字符串转MD5", bg="lightblue", width=10,command=self.str_trans_to_md5)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=1, column=11)
        self.str_trans_to_md5_button = Button(self.init_window_name, text="RSA签密", bg="lightblue", width=10,command=self.enRSA)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=2, column=11)
        self.str_trans_to_md5_button = Button(self.init_window_name, text="ECC签密", bg="lightblue", width=10,command=self.enECC)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=3, column=11)
        self.str_trans_to_md5_button = Button(self.init_window_name, text="ECCAES签密", bg="lightblue", width=10,command=self.enECCAES)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=4, column=11)
        self.str_trans_to_md5_button = Button(self.init_window_name, text="DH签密", bg="lightblue", width=10,command=self.enDH)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=5, column=11)
        self.str_trans_to_md5_button = Button(self.init_window_name, text="RSAAES签密", bg="lightblue", width=10,command=self.enRSAAES)  # 调用内部方法  加()为直接调用
        self.str_trans_to_md5_button.grid(row=6, column=11)



    #功能函数
    def str_trans_to_md5(self):
        src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
        #print("src =",src)
        if src:
            try:
                myMd5 = hashlib.md5()
                myMd5.update(src)
                myMd5_Digest = myMd5.hexdigest()
                #print(myMd5_Digest)
                #输出到界面
                self.result_data_Text.delete(1.0,END)
                self.result_data_Text.insert(1.0,myMd5_Digest)
                self.write_log_to_Text("INFO:str_trans_to_md5 success")
            except:
                self.result_data_Text.delete(1.0,END)
                self.result_data_Text.insert(1.0,"字符串转MD5失败")
        else:
            self.write_log_to_Text("ERROR:str_trans_to_md5 failed")

    def enRSA(self):
        m = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
        m=int(m)
        alicedone = RSA.KGC(512)
        bobdone =RSA.KGC(512)
        params = RSA.Params(alicedone.P, bobdone.P, 512)
        # print(alicedone.P,bobdone.P)
        alice = RSA.User("Alice", alicedone.pk, alicedone.P, params.bits)
        bob = RSA.User("Bob", bobdone.pk, bobdone.P, params.bits)
        Signcryption_text = alice.signcrypt(bob, params, m, alicedone.sk)
        self.result_data_Text.delete(1.0, END)
        St="h1:"+str(Signcryption_text[0])+"\nt:"+str(Signcryption_text[1])+"\nc:"+str(Signcryption_text[2])+"\ns:"+str(Signcryption_text[3])
        self.result_data_Text.insert(1.0, "签密结果\n")
        self.result_data_Text.insert(1.0, St)
        key="alice.sk="+str(alicedone.sk)+"\nalice.pk="+str(alicedone.pk)+"\nbob.sk="+str(bobdone.sk)+"\nbob.pk="+str(bobdone.pk)
        self.write_log_to_Text(key)

    def enDH(self):
        m = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
        m = int(m)
        kgc_1 = DH.KGC(512)
        params = DH.Params(kgc_1.p, kgc_1.q, kgc_1.g, kgc_1.p_pub)
        # print("p=",kgc_1.p)
        # print("q=",)
        Alice = DH.User('Alice', params)
        Bob = DH.User('Bob', params)
        kgc_1.partialkey_compute(Alice)
        kgc_1.partialkey_compute(Bob)
        Signcryption_text = Alice.signcrypt(Bob, params, m)
        St = "R:" + str(Signcryption_text[0]) + "\nC:" + str(Signcryption_text[1]) + "\nS:" + str(Signcryption_text[2])
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, St)
        key = "alice.sk=" + str(Alice.X_u) + "\nalice.pk=" + str(Alice.Y_u) + "\nbob.sk=" + str(Bob.X_u) + "\nbob.pk=" + str(Bob.Y_u)
        m_un = Bob.unsigncrypt(Alice, params, Signcryption_text)
        self.write_log_to_Text(key)
        print(m_un)


    def enECC(self):
        m = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
        m = int(m)
        alicedone = ECC.KGC()
        bobdone = ECC.KGC()
        params = ECC.Params(alicedone.pk, bobdone.pk)
        alice = ECC.User("Alice", alicedone.pk)
        bob = ECC.User("Bob", bobdone.pk)
        Signcryption_text = alice.signcrypt(bob, params, m, alicedone.sk)
        St = "h1:" + str(Signcryption_text[0]) + "\nt:" + str(Signcryption_text[1]) + "\nc:" + str(
        Signcryption_text[2]) + "\ns:" + str(Signcryption_text[3])
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, "签密结果\n")
        self.result_data_Text.insert(1.0, St)
        print(Signcryption_text)
        key = "alice.sk=" + str(alicedone.sk) + "\nalice.pk=" + str(alicedone.pk) + "\nbob.sk=" + str(bobdone.sk) + "\nbob.pk=" + str(bobdone.pk)
        self.write_log_to_Text(key)
        #m = bob.unsigncrypt(alice, Signcryption_text, bobdone.sk)
        #print(m)

    def enECCAES(self):
        m = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
        alicedone = ECCAES.KGC()
        bobdone = ECCAES.KGC()
        params = ECCAES.Params(alicedone.pk, bobdone.pk)
        alice = ECCAES.User("Alice", alicedone.pk)
        bob = ECCAES.User("Bob", bobdone.pk)
        m = "hello wolrd!"
        Signcryption_text = alice.signcrypt(bob, params, m, alicedone.sk)
        St = "h1:" + str(Signcryption_text[0]) + "\nt:" + str(Signcryption_text[1]) + "\nc:" + str(
            Signcryption_text[2]) + "\ns:" + str(Signcryption_text[3])+"\nc_text:"+str(Signcryption_text[4])
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, "签密结果\n")
        self.result_data_Text.insert(1.0, St)
        key = "alice.sk=" + str(alicedone.sk) + "\nalice.pk=" + str(alicedone.pk) + "\nbob.sk=" + str(
            bobdone.sk) + "\nbob.pk=" + str(bobdone.pk)
        self.write_log_to_Text(key)
    #获取当前时间

    def enRSAAES(self):
        m = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
        alicedone = RSAAES.KGC(512)
        bobdone = RSAAES.KGC(512)
        params = RSAAES.Params(alicedone.P, bobdone.P, 512)
        # print(alicedone.P,bobdone.P)
        alice = RSAAES.User("Alice", alicedone.pk, alicedone.P, params.bits)
        bob = RSAAES.User("Bob", bobdone.pk, bobdone.P, params.bits)
        m = "hello world!"
        Signcryption_text = alice.signcrypt(bob, params, m, alicedone.sk)
        St = "h1:" + str(Signcryption_text[0]) + "\nt:" + str(Signcryption_text[1]) + "\nc:" + str(
            Signcryption_text[2]) + "\ns:" + str(Signcryption_text[3])+"\nc_text:"+str(Signcryption_text[4])
        # print(Signcryption_text)
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, "\n签密结果\n")
        self.result_data_Text.insert(1.0, St)
        key = "alice.sk=" + str(alicedone.sk) + "\nalice.pk=" + str(alicedone.pk) + "\nbob.sk=" + str(
            bobdone.sk) + "\nbob.pk=" + str(bobdone.pk)
        self.write_log_to_Text(key)
        m = bob.unsigncrypt(alice, params, Signcryption_text, bobdone.sk)
        print(m)
    def get_current_time(self):
        current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        return current_time


    #日志动态打印
    def write_log_to_Text(self,logmsg):
        global LOG_LINE_NUM
        current_time = self.get_current_time()
        logmsg_in = str(current_time) +" " + str(logmsg) + "\n"      #换行
        if LOG_LINE_NUM <= 7:
            self.log_data_Text.insert(END, logmsg_in)
            LOG_LINE_NUM = LOG_LINE_NUM + 1
        else:
            self.log_data_Text.delete(1.0,2.0)
            self.log_data_Text.insert(END, logmsg_in)


def gui_start():
    init_window = Tk()              #实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()

    init_window.mainloop()          #父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


gui_start()